import java.util.Arrays;

public class TestDemo {
    public static void main1(String[] args) {
//        冒泡排序
        int[] array = {3,1,5,2,7,4,6,8,9};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]>array[j+1]){
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(array));
    }

    public  static boolean ifsort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            if(arr[i]>arr[i+1]){
                return false;
            }
        }
        return true;
    }
    public static void main2(String[] args) {
//        给定一个整型数组, 判定数组是否有序（递增）
        int[] arr = {2, 3, 4, 41, 5, 6};
        boolean n = ifsort(arr);
        if (n) {
            System.out.println("有序");
        } else {
            System.out.println("无序");
        }
    }

    public static int binary_search(int[] arr,int k) {
        int n = arr.length;
        int left = 0;
        int right = n-1;
        while(left<=right){
            int mid=left+(right-left)/2;
            if(arr[mid]>k){
                right = mid-1;
            } else if (arr[mid]<k) {
                left = mid+1;
            }else {
                return mid;
            }
        }
        return 0;
    }
    public static void main(String[] args) {
//        二分查找
        int[] arr = {1,2,3,4,5,6,7,8,9};
        int k = 6;
        int left = 0;
        int right = arr.length-1;
        System.out.println(binary_search(arr,k));
    }
}

